//
// Created by JZYFC on 2021/12/21.
//

#include "NoteWnd.h"
#include "FontUtil.h"

#define ID_CONFIRM_BTN 0xd0
#define ID_EDITBOX 0xc0

#define ID_CANCEL_BTN 0xd1

#define NOTE_WIDTH 540
#define NOTE_HEIGHT 400

LRESULT CALLBACK NoteProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    HDC hdc;
    static PAINTSTRUCT ps;
    static TEXTMETRIC text_metric;
    static HFONT font;

    static RECT edit;

    switch (msg) {
        case WM_CREATE:

            edit.left = 22;
            edit.top = 32;
            edit.right = NOTE_WIDTH - 22;
            edit.bottom = NOTE_HEIGHT - 56;

            // Confirm btn
            HWND hConfirmBtn = CreateWindowEx(
                    WS_EX_LEFT,
                    L"Button",
                    L"确定",
                    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                    312,
                    352,
                    144,
                    40,
                    hWnd,
                    (HMENU) ID_CONFIRM_BTN,
                    (HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                    NULL
            );
            // CancelBtn
            HWND hCancelBtn = CreateWindowEx(
                    WS_EX_LEFT,
                    L"Button",
                    L"取消",
                    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                    72,
                    352,
                    144,
                    40,
                    hWnd,
                    (HMENU) ID_CANCEL_BTN,
                    (HINSTANCE) GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                    NULL
            );


            hdc = GetDC(hWnd);

            SelectObject(hdc, font);

            GetTextMetrics(hdc, &text_metric);
            HWND hEditBox = CreateWindowEx(
                    0, L"EDIT",
                    NULL,
                    WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER| WS_TABSTOP | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN,
                    edit.left + 1,
                    edit.top + 1,
                    edit.right - edit.left,
                    edit.bottom - edit.top,
                    hWnd,
                    (HMENU)ID_EDITBOX,
                    (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                    NULL
            );

            font = (HFONT) GetStockObject(DEFAULT_GUI_FONT);

            ReleaseDC(hWnd, hdc);

            EnumChildWindows(hWnd, EnumChildWndSetFont, (LPARAM)font);
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);

            SelectObject(hdc, font);
            edit.top -= text_metric.tmHeight + 6;
            DrawText(hdc, L"笔记", -1, &edit, DT_LEFT | DT_VCENTER);

            EndPaint(hWnd, &ps);
            break;
        case WM_COMMAND:
            switch (LOWORD(wParam)) {
                case ID_CANCEL_BTN:
                    DestroyWindow(hWnd);
                    break;
                case ID_CONFIRM_BTN:

                    break;
            }
            break;
        case WM_CLOSE:
            DestroyWindow(hWnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
}

void open_note_window(HWND hParent) {

    EnableWindow(hParent, false);

    LPCWSTR lpWindowName = L"笔记";
    LPCWSTR lpClassName = L"mWndNoteClass";

    HWND hNoteWnd = CreateWindowEx(
            WS_EX_LEFT, lpClassName,
            lpWindowName,
            WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX,
            120,
            120,
            NOTE_WIDTH + 16,
            NOTE_HEIGHT + 39,
            hParent,
            NULL,
            (HINSTANCE) GetWindowLongPtr(hParent, GWLP_HINSTANCE),
            NULL
    );

    // Display window
    ShowWindow(hNoteWnd, SW_SHOW);
    UpdateWindow(hNoteWnd);

    // Msg loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    EnableWindow(hParent, true);
    SetForegroundWindow(hParent);
}